/**
 * 获取节点上下左右连接点的位置
 * @param node
 */
function buildSocket(node) {
  node.s_top = {
    x: node.x + node.w / 2,
    y: node.y
  };
  node.s_right = {
    x: node.x + node.w,
    y: node.y + node.h / 2
  };
  node.s_bottom = {
    x: node.x + node.w / 2,
    y: node.y + node.h
  };
  node.s_left = {
    x: node.x,
    y: node.y + node.h / 2
  };
}

function getDistance(p0, p1) {
  var lx = p0.x - p1.x, ly = p0.y - p1.y;
  return Math.sqrt(lx * lx + ly * ly);
}

/**
 * 寻找两个节点之间最近的连接方式
 * @param a 起点
 * @param b 终点
 * @returns 起点的连接点-终点的连接点，如 top-bottom
 */
function findNearSockets(a, b) {
  const sockets = ['top', 'right', 'bottom', 'left'];
  let minLong = -1;
  let result = null;
  for (let i = 0; i < sockets.length; i++) {
    const startSocket = sockets[i];
    for (let j = 0; j < sockets.length; j++) {
      if (i === j) { //不可能同边相连，如top连top，left连left
        continue;
      }
      const endSocket = sockets[j];
      const long = getDistance(a['s_' + startSocket], b['s_' + endSocket]);
      if (minLong === -1 || long < minLong) { //获取距离最短的连接方式
        minLong = long;
        result = startSocket + '-' + endSocket;
      }
    }
  }
  return result;
}


function svgLine(startNode, endNode) {
  const a = Object.assign({}, startNode);
  const b = Object.assign({}, endNode);
  buildSocket(a);
  buildSocket(b);

  let socketType = findNearSockets(a, b);
  // console.log('socketType', socketType);
  // socketType = 'left-bottom';
  let svgData = {};
  if (socketType === 'top-bottom') {
    svgData = lineTopBottom(a, b);
  } else if (socketType === 'top-left') {
    svgData = lineTopLeft(a, b);
  } else if (socketType === 'top-right') {
    svgData = lineTopRight(a, b);
  } else if (socketType === 'right-bottom') {
    svgData = lineRightBottom(a, b);
  } else if (socketType === 'right-left') {
    svgData = lineRightLeft(a, b);
  } else if (socketType === 'left-bottom') {
    svgData = lineLeftBottom(a, b);
  }
  // 与上面相反的，a、b交换位置即可
  else if (socketType === 'right-top') {
    svgData = lineTopRight(b, a);
  } else if (socketType === 'left-top') {
    svgData = lineTopLeft(b, a);
  } else if (socketType === 'left-right') {
    svgData = lineRightLeft(b, a);
  } else if (socketType === 'bottom-top') {
    svgData = lineTopBottom(b, a);
  } else if (socketType === 'bottom-left') {
    svgData = lineLeftBottom(b, a);
  } else if (socketType === 'bottom-right') {
    svgData = lineRightBottom(b, a);
  }
  svgData.style = 'fill:none;stroke:#1e82fa;stroke-width:3';

  //svg整体向外偏移10像素，因为线有宽度，贴边的线显示的宽度不够
  svgData.x -= 10;
  svgData.y -= 10;
  svgData.w += 20;
  svgData.h += 20;
  if (svgData.points) {
    svgData.points.forEach(p => {
      p[0] += 10;
      p[1] += 10;
    });
    svgData.points = svgData.points.map(p => p.join(',')).join(' ');
  }
  return svgData;
}

function lineTopBottom(a, b) {
  const s1 = a.s_top;
  const s2 = b.s_bottom;

  const w = Math.abs(s2.x - s1.x);
  const h = s1.y - s2.y;

  let points = null;
  if (s1.x > s2.x) {
    points = [[w, h], [w, h / 2], [0, h / 2], [0, 0]]
  } else {
    points = [[0, h], [0, h / 2], [w, h / 2], [w, 0]]
  }

  return {
    x: Math.min(s1.x, s2.x),
    y: b.y + b.h,
    w: w,
    h: h,
    points: points,
  };
}

function lineTopLeft(a, b) {
  const s1 = a.s_top;
  const s2 = b.s_left;

  const w = s2.x - s1.x;
  const h = s1.y - s2.y;
  return {
    x: s1.x,
    y: s2.y,
    w: w,
    h: h,
    points: [[0, h], [0, 0], [w, 0]],
  };
}

function lineTopRight(a, b) {
  const s1 = a.s_top;
  const s2 = b.s_right;

  const w = s1.x - s2.x;
  const h = s1.y - s2.y;
  return {
    x: s2.x,
    y: s2.y,
    w: w,
    h: h,
    points: [[w, h], [w, 0], [0, 0]],
  };
}

function lineRightLeft(a, b) {
  const s1 = a.s_right;
  const s2 = b.s_left;

  const w = s2.x - s1.x;
  const h = Math.abs(s2.y - s1.y);

  let points = null;
  if (s1.y > s2.y) {
    points = [[0, h], [w / 2, h], [w / 2, 0], [w, 0]]
  } else {
    points = [[0, 0], [w / 2, 0], [w / 2, h], [w, h]]
  }

  return {
    x: s1.x,
    y: Math.min(s1.y, s2.y),
    w: w,
    h: h,
    points: points,
  };
}

function lineRightBottom(a, b) {
  const s1 = a.s_right;
  const s2 = b.s_bottom;

  const w = Math.abs(s2.x - s1.x);
  const h = Math.abs(s1.y - s2.y);
  return {
    x: s1.x,
    y: s2.y,
    w: w,
    h: h,
    points: [[0, h], [w, h], [w, 0]],
  };
}

function lineLeftBottom(a, b) {
  const s1 = a.s_left;
  const s2 = b.s_bottom;

  const w = Math.abs(s2.x - s1.x);
  const h = Math.abs(s1.y - s2.y);
  return {
    x: s2.x,
    y: s2.y,
    w: w,
    h: h,
    points: [[0, 0], [0, h], [w, h]],
  };
}


export default svgLine;
